package com.newbsd.web.maventest.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.newbsd.web.maventest.dao.PageDao;
import com.newbsd.web.maventest.dao.impl.PageDaoImpl;
import com.newbsd.web.maventest.pojo.Order;
import com.newbsd.web.maventest.pojo.Ticket;
import com.newbsd.web.maventest.service.impl.OrdersServiceImpl;
import com.newbsd.web.maventest.service.impl.TicketServiceImpl1;
import com.newbsd.web.maventest.util.pageUtil;

/**
 * Servlet implementation class Orders
 */
@WebServlet("/Orders")
public class OrdersController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrdersController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
		OrdersServiceImpl ordersServiceImpl = new OrdersServiceImpl();
		TicketServiceImpl1 ticketServiceImpl = new TicketServiceImpl1();
		Order order = new Order();
		Ticket ticket = new Ticket();
		ordersServiceImpl.createOrders(order);
	    ticketServiceImpl.updateTicket1(ticket);
		ordersServiceImpl.detailOrders(order);
		
		/**
		 * 分页
		 * 每页显示5条数据
		 */
		int pageSize = 5;
		int totalCount = 0;
		PageDao pageDaoImpl = new PageDaoImpl();
		pageUtil page = new pageUtil(pageSize, totalCount);
		page = 	pageDaoImpl.getPage(page.getPageNo(), page.getPageSize());
		page.setPageNo(page.getPageNo());
		request.setAttribute("page", page);
		
		request.setAttribute("username", order.getUsername());
		request.setAttribute("cert", order.getCert());
		request.setAttribute("orderid", order.getOrderid());
		request.setAttribute("ticketid", order.getTicketid());
		request.setAttribute("trainno", order.getTrainno());
		request.setAttribute("price", order.getPrice());
		request.setAttribute("origin", order.getOrigin());
		request.setAttribute("destination", order.getDestination());
		request.setAttribute("pstatus", order.getPstatus());
		request.setAttribute("cstatus", order.getCstatus());
		
		
		request.getRequestDispatcher("orders/myoders.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
